// file: 	cs-mech.do
// purpose:	test relationship between district size and hypothesized causal
//				mechanisms, using the 2008 National Annenberg Election Study
//				and Theil's H indices from the 2010 ACS. 

set more off
clear

* Prior to running code, user should download the 2008 NAES survey data
*		in Stata format (will need to request access from the Annenberg Public 
*		Policy Center (https://www.annenbergpublicpolicycenter.org/2008-naes-telephone-and-online-data-sets/)

use stmerge.dta, clear
keep if year==2008
save stmerge2008.dta, replace


* must have NAES 2008 Online component survey downloaded (all waves). User 
* should convert from native SPSS format to .dta format 
use naes08-online-compact.dta, clear

rename WFc01_a fips
merge m:1 fips using stmerge2008.dta, gen(mg_stmerge)
gen lncssen = ln(cssen)
recode KH06rB_b (1=1 "Yes")(2=0 "No")(999=.), gen(personal_st)
recode KH06rA_b (1=1 "Yes")(2=0 "No")(999=.), gen(personal_cd)
recode KH06rC_b (1=1 "Yes")(2=0 "No")(999=.), gen(personal_loc)

tab1 personal_st personal_cd personal_loc

reg personal_st i.fips
predict p, xb
predict se, stdp
gen ci_up = p + 1.98*se
gen ci_lo = p - 1.98*se

pwcorr personal_st personal_cd lncs lncssen lnpop cshouse cssen, sig

* Create Fig 6. 

twoway scatter p lncs , msize(tiny) mcolor(gs7) ||  ///
	rspike ci_up ci_lo lncs, lcolor(gs7) lwidth(vvthin) || ///
	lowess personal_st lncs, color(black) lwidth(medthick) ///
	plotregion(color(none)) graphregion(color(white)) ///
	xtitle("Constituency Size (log)", size(small)) ///
	xlab(-1(1)4, labsize(vsmall)) ///
	ytitle("Proportion Personally Aquainted with Legislator", size(small))  ///
	ylab(0(.1).4, angle(0) labsize(vsmall)) ///
	legend(off) ysize(3) xsize(5)
	
graph export personal.pdf, replace

replace size_low = size_low/100
replace size_up = size_up/100

label var lncs "Lower Chamber Constituency Size (logged)"
label var lncs "Lower Chamber Constituency Size (logged)"
label var lncssen "Upper Chamber Constituency Size (logged)"
label var lnpop "Population Size in millions (logged)"
label var size_low "Lower Chamber Size (in hundreds)"
label var size_up "Upper Chamber Size (in hundreds)"
label var personal_cd "Personally acquainted with member of Congress"
label var personal_loc "Personally acquainted with member of local gov"


* These models (linear multilevel models) test the relationship between 
* 	personally knowing a member of the legislature and district size,
*	at the survey-respondent level. Creates Table OA7.

xtset fips
xtreg personal_st lncs lncssen, re
est store m1 
xtreg personal_st  lncs lncssen if fips~=33, re
est store m1b 
xtreg personal_st  lnpop size_low size_up, re
est store m2 
xtreg personal_st  lnpop size_low size_up if fips~=33, re
est store m2b 
xtreg personal_st  lnpop size_low size_up personal_cd personal_loc, re
est store m3 
xtreg personal_st  lnpop size_low size_up personal_cd personal_loc if fips~=33, re
est store m3b 

esttab m1 m1b m2 m2b m3 m3b using tableOA7.txt, cells("b(fmt(3) star)" "se(fmt(3) par)") ///
	starlevels(+ .1 * .05 ** .01) tex stats(N N_clust r2_b r2_w r2_o) style(tex) ///
	label nolz order(lncs lncssen lnpop size_low size_up personal_cd personal_loc)  replace

	
* Aggregate to the state level	
collapse p* lncs lnpop size_low , by(fips)

reg personal_st lncs if fips~=33, cluster(fips)
est store per_1
reg personal_st lnpop size_low if fips~=33, cluster(fips)
est store per_2
reg personal_st lnpop size_low personal_loc if fips~=33, cluster(fips)
est store per_3
reg personal_st lncs personal_loc if fips~=33, cluster(fips)
est store per_4
	
drop lncs lnpop size_low 

save personal.dta, replace

use entropy_st.dta, clear
gen year=2008
joinby stateabv using stateidentifiers.dta
merge m:1 fips using stmerge2008.dta, gen(stmerge)
replace size_low = size_low/100
replace size_up = size_up/100


foreach var in race faminc age ed fb {
reg H_`var'_l lncs if stateabv~="NH"
est store `var'_1
reg H_`var'_l lnpop size_low if stateabv~="NH" 
est store `var'_2
reg H_`var'_l lnpop size_low H_`var'_ct if stateabv~="NH" 
est store `var'_3
reg H_`var'_l lncs H_`var'_ct if stateabv~="NH" 
est store `var'_4
}


* Create Table 5
esttab per_3 race_3 faminc_3 age_3 ed_3 fb_3 using table5.txt, cells("b(fmt(3) star)" "se(fmt(3) par)") ///
	starlevels(+ .1 * .05 ** .01) tex stats(N  r2) style(tex) ///
	label nolz order(lnpop size_lower size_upper personal_cd personal_loc H_*)  replace

	
* Replicates Table 5 with logged constituency size	
esttab per_4 race_4 faminc_4 age_4 ed_4 fb_4 using table5.txt, cells("b(fmt(3) star)" "se(fmt(3) par)") ///
	starlevels(+ .1 * .05 ** .01) tex stats(N  r2) style(tex) ///
	label nolz order(lnpop size_lower size_upper personal_cd personal_loc H_*)  append


